package com.accesshollywood.grfxOrdering.Dialogs;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import com.accesshollywood.grfxOrdering.basics.NamedList;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.indexables.items.GrfxItem;

public class NewItemDialog extends JDialog {
	public enum ITEM_MODE {
		NEW_ITEM,
		EDIT_ITEM,
		ORIGINAL_ITEM;
	};

	private final JPanel	contentPanel	= new JPanel();
	private EditOrderDialog	newOrderDialog;
	private JTextArea		txtFootage;
	private JTextArea		txtText;
	private JTextArea		txtStills;
	private JTextArea		txtFilename;

	private GrfxItem		currItem;
	public ITEM_MODE				mode;

	public NewItemDialog(EditOrderDialog newOrderDialog) {
		super();
		mode = ITEM_MODE.NEW_ITEM;
//		setModal(true);
		currItem = new GrfxItem();
		this.newOrderDialog = newOrderDialog;
		createDialog();
	}

	public NewItemDialog(EditOrderDialog newOrderDialog, GrfxItem currItem) {
		super();
		mode = ITEM_MODE.EDIT_ITEM;
//		setModal(true);
		this.currItem = currItem;
		this.newOrderDialog = newOrderDialog;
		createDialog();
	}

	public void createDialog() {
		setBounds(100, 100, 493, 442);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
		if(Users.currentUser.isAdmin() || Users.currentUser.isOrderer())
		{
			JPanel filenamePanel = new JPanel();
			filenamePanel.setBorder(new TitledBorder(null, "FileName", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			contentPanel.add(filenamePanel);
			{
				txtFilename = new JTextArea();
				filenamePanel.add(txtFilename);
				txtFilename.setColumns(55);
				txtFilename.setRows(1);
			}
		}
		if(Users.currentUser.isAdmin() || Users.currentUser.isOrderer())
		{
			JPanel textPanel = new JPanel();
			textPanel.setAlignmentY(Component.TOP_ALIGNMENT);
			textPanel.setBorder(new TitledBorder(null, "Text", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			contentPanel.add(textPanel);
			textPanel.setLayout(new BorderLayout(10, 10));
			{
				txtText = new JTextArea(5, 40);
			}
			{
				JScrollPane scrollPane = new JScrollPane(txtText);
				scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				textPanel.add(scrollPane);
			}
		}
		{
			Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
			contentPanel.add(rigidArea);
		}
		if(Users.currentUser.isAdmin() || Users.currentUser.isOrderer() || Users.currentUser.isPhotoEdit())
		{
			JPanel stillsPanel = new JPanel();
			stillsPanel.setBorder(new TitledBorder(null, "Stills", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			contentPanel.add(stillsPanel);
			stillsPanel.setLayout(new BorderLayout(0, 0));
			{
				txtStills = new JTextArea(5, 40);
			}
			{
				JScrollPane scrollPane = new JScrollPane(txtStills);
				scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				stillsPanel.add(scrollPane);
			}
		}

		{
			Component rigidArea = Box.createRigidArea(new Dimension(20, 20));
			contentPanel.add(rigidArea);
		}

		if(Users.currentUser.isAdmin() || Users.currentUser.isOrderer())
		{
			JPanel footagePanel = new JPanel();
			footagePanel.setBorder(new TitledBorder(null, "Footage", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			contentPanel.add(footagePanel);
			footagePanel.setLayout(new BorderLayout(0, 0));
			{
				txtFootage = new JTextArea(5, 40);
			}
			{
				JScrollPane scrollPane = new JScrollPane(txtFootage);
				scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
				footagePanel.add(scrollPane);
			}
		}

		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						OK();
					}
				});
//				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						cancel();
					}
				});
//				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}

		setLocationRelativeTo(null);

		this.setVisible(true);
	}

	public void OK() {
		newOrderDialog.addItem(makeItem(), mode);
		this.dispose();
	}

	public void cancel() {
		this.dispose();
	}

	public GrfxItem makeItem() {
		GrfxItem newItem;
		if(mode == ITEM_MODE.NEW_ITEM){
			newItem = new GrfxItem();
		}else{
			newItem = currItem;
		}

		newItem.setFilename(txtFilename.getText());

		if (!txtText.getText().equals("")) {
//			newItem.setTextList(txtText);
			newItem.setNeedsResearch(true);
		}
		if (!txtStills.getText().equals("")) {
//			newItem.setStillList(txtStills);
			newItem.setNeedsPhotoEdit(true);
		}

		if (!txtFootage.getText().equals("")) {
//			newItem.setFootageList(txtFootage);
			newItem.setNeedsFootage(true);
		}
		return newItem;
	}
}
